Option Strict Off
Option Explicit On
Module basConvert
	Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)
	
	' basConvert: Utilities to convert between byte arrays, hex strings,
	' strings containing binary values, and 32-bit word arrays.
	
	' Version 1. Published January 2002
	'************************* COPYRIGHT NOTICE*************************
	' This code was originally written in Visual Basic by David Ireland
	' and is copyright (c) 2000-2 D.I. Management Services Pty Limited,
	' all rights reserved.
	
	' You are free to use this code as part of your own applications
	' provided you keep this copyright notice intact.
	
	' This code may only be used as part of an application. It may
	' not be reproduced or distributed separately by any means without
	' the express written permission of the author.
	
	' David Ireland and DI Management Services Pty Limited make no
	' representations concerning either the merchantability of this
	' software or the suitability of this software for any particular
	' purpose. It is provided "as is" without express or implied
	' warranty of any kind.
	
	' Please forward comments or bug reports to <code@di-mgt.com.au>.
	' The latest version of this source code can be downloaded from
	' www.di-mgt.com.au/crypto.html.
	'****************** END OF COPYRIGHT NOTICE*************************
	
	' The Public Functions in this module are:
	' cv_BytesFromHex(sInputHex): Returns array of bytes
	' cv_BytesFromString(strInput): Returns array of bytes
	' cv_WordsFromHex(sHex): Returns array of words (Longs)
	' cv_HexFromWords(aWords): Returns hex string
	' cv_HexFromBytes(aBytes()): Returns hex string
	' cv_HexFromString(str): Returns hex string
	' cv_StringFromHex(strHex): Returns string of ascii characters
	' cv_GetHexByte(sInputHex, iIndex): Extracts iIndex'th byte from hex string
	' RandHexByte(): Returns random byte as a 2-digit hex string
    Friend Function cv_arrayFromString(ByRef str As String) As String()
        Dim f_str() As String = str.Split(",")
        Return f_str
    End Function
    Public Function cv_BytesFromHex(ByVal sInputHex As String) As Byte()
        ' Returns array of bytes from hex string in big-endian order
        ' E.g. sHex="FEDC80" will return array {&HFE, &HDC, &H80}
        Dim i As Integer
        Dim M As Integer
        Dim aBytes() As Byte
        If Len(sInputHex) Mod 2 <> 0 Then
            sInputHex = "0" & sInputHex
        End If

        M = Len(sInputHex) \ 2
        ReDim aBytes(M - 1)

        For i = 0 To M - 1
            aBytes(i) = Val("&H" & Mid(sInputHex, i * 2 + 1, 2))
        Next

        'UPGRADE_WARNING: Couldn't resolve default property of object cv_BytesFromHex. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        Return aBytes
    End Function
	
    Public Function cv_BytesFromString(ByRef strInput As String) As Byte()
        ' Converts string <strInput> of ascii chars to array of bytes
        ' str may contain chars of any value between 0 and 255.
        ' E.g. strInput="abc." will return array {&H61, &H62, &H63, &H2E}
        Dim i As Integer
        Dim M As Integer
        Dim aBytes() As Byte

        M = Len(strInput)
        ReDim aBytes(M - 1)

        For i = 0 To M - 1
            aBytes(i) = Asc(Mid(strInput, i + 1, 1))
        Next

        'UPGRADE_WARNING: Couldn't resolve default property of object cv_BytesFromString. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        Return aBytes

    End Function
	
    Public Function cv_WordsFromHex(ByVal sHex As String) As Int64()
        ' Converts string <sHex> with hex values into array of words (long ints)
        ' E.g. "fedcba9876543210" will be converted into {&HFEDCBA98, &H76543210}
        Const ncLEN As Short = 8
        Dim i As Int64
        Dim nWords As Int64
        Dim aWords() As Int64

        nWords = Len(sHex) \ ncLEN
        ReDim aWords(nWords - 1)
        For i = 0 To nWords - 1
            aWords(i) = Val("&H" & Mid(sHex, i * ncLEN + 1, ncLEN))
        Next

        'UPGRADE_WARNING: Couldn't resolve default property of object cv_WordsFromHex. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        Return aWords

    End Function
	
	Public Function cv_HexFromBytes(ByRef aBytes() As Byte) As String
		' Returns hex string from array of bytes
		' E.g. aBytes() = {&HFE, &HDC, &H80} will return "FEDC80"
        'Dim i As Integer
        ' Dim iIndex As Integer
        Dim f_return As String = ""

        For Each by As Byte In aBytes
            f_return &= HexFromByte(by)
        Next

        Return f_return
        'cv_HexFromBytes = New String(" ", (UBound(aBytes) - LBound(aBytes) + 1) * 2)
        'iIndex = 0
        'For i = LBound(aBytes) To UBound(aBytes)
        'Mid(cv_HexFromBytes, iIndex + 1, 2) = HexFromByte(aBytes(i))
        'iIndex += iIndex + 2
        'Next 
		
	End Function
	Public Function cv_StrFromBytes(ByRef aBytes() As Byte) As String
		' Returns hex string from array of bytes
		' E.g. aBytes() = {&HFE, &HDC, &H80} will return "FEDC80"
		Dim i As Integer
		Dim iIndex As Integer
		
        cv_StrFromBytes = New String(" ", (UBound(aBytes) - LBound(aBytes) + 1) * 3)
		iIndex = 0
		For i = LBound(aBytes) To UBound(aBytes)
			Mid(cv_StrFromBytes, iIndex + 1, 2) = Chr(aBytes(i))
            iIndex = iIndex + 3
		Next 
	End Function
	
	'UPGRADE_NOTE: str was upgraded to str_Renamed. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
	Public Function cv_HexFromString(ByRef str_Renamed As String) As String
		' Converts string <str> of ascii chars to string in hex format
		' str may contain chars of any value between 0 and 255.
		' E.g. "abc." will be converted to "6162632E"
		Dim byt As Byte
		Dim i As Integer
		Dim n As Integer
		Dim iIndex As Integer
		Dim sHex As String
		
		n = Len(str_Renamed)
		sHex = New String(" ", n * 2)
		iIndex = 0
		For i = 1 To n
			byt = CByte(Asc(Mid(str_Renamed, i, 1)) And &HFFs)
			Mid(sHex, iIndex + 1, 2) = HexFromByte(byt)
			iIndex = iIndex + 2
		Next 
		cv_HexFromString = sHex
		
	End Function
	
	Public Function cv_StringFromHex(ByRef strHex As String) As String
		' Converts string <strHex> in hex format to string of ascii chars
		' with value between 0 and 255.
		' E.g. "6162632E" will be converted to "abc."
		Dim i As Short
		Dim nBytes As Short
		
		nBytes = Len(strHex) \ 2
		cv_StringFromHex = New String(" ", nBytes)
		For i = 0 To nBytes - 1
			Mid(cv_StringFromHex, i + 1, 1) = Chr(Val("&H" & Mid(strHex, i * 2 + 1, 2)))
		Next 
		
	End Function
	
	Public Function cv_GetHexByte(ByVal sInputHex As String, ByRef iIndex As Integer) As Byte
		' Extracts iIndex'th byte from hex string (starting at 1)
		' E.g. cv_GetHexByte("fecdba98", 3) will return &HBA
		Dim i As Integer
		i = 2 * iIndex
		If i > Len(sInputHex) Or i <= 0 Then
			cv_GetHexByte = 0
		Else
			cv_GetHexByte = Val("&H" & Mid(sInputHex, i - 1, 2))
		End If
		
	End Function
	
	Public Function RandHexByte() As String
		'   Returns a random byte as a 2-digit hex string
		Static stbInit As Boolean
		If Not stbInit Then
			Randomize()
			stbInit = True
		End If
		
		RandHexByte = HexFromByte(CByte((Rnd() * 256) And &HFFs))
	End Function
	
	Private Function ChrFromByte(ByVal x As Object) As String
		'UPGRADE_WARNING: Couldn't resolve default property of object x. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		ChrFromByte = Chr(x)
	End Function
	Private Function HexFromByte(ByVal x As Object) As String
        x = x And &HFFS
        If x < 16 Then
            HexFromByte = "0" & Hex(x)
        Else
            HexFromByte = Hex(x)
        End If
    End Function
		
End Module